/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package com.cyn.controller;


import com.cyn.aframework.utils.R;
import com.cyn.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 系统配置信息
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/redis")
public class RedisTestController {


	@Autowired
	RedisService redisService;

	@GetMapping("/setRedis")
	public R setRedis(){

		int index=0;
		Long begin = System.currentTimeMillis();
//		while(index>=0) {
//			index++;
//			if(index%10000==0){
//				Long end = System.currentTimeMillis();
//				System.out.println("处理数据中==>插入数据总条数size:"+index+"  total use:"+
//						(end-begin)/1000+"s,处理速度为:"+(index/((end-begin)/1000))+"条/s");
//			}
//			redisService.saveOrUpdate(String.valueOf(index));
//		}

		redisService.saveOrUpdate("hhh");
		return R.ok();
	}

	@GetMapping("/getRedis")
	public R getRedis(){
		String temp = redisService.get("123");
		R.ok().put("redis",temp);
		R r = new R();
		r.put("redis",temp);
		return r;
	}


	@GetMapping("lockTest")
	public R lockTest(){
		for (int i = 0; i < 1000; i++) {
			new Thread(()->{
				if(redisService.getLock()) {
					System.out.println("******"+Thread.currentThread().getName() + "获得锁");
					redisService.unLock();
					System.out.println(Thread.currentThread().getName() + "释放锁");
				}else{
					System.out.println(Thread.currentThread().getName() + "未获得锁");
				}
			}).start();
		}
		return R.ok();
	}
}
